package com.hoperun.nocserver.common.utils;

/**
 * Unicode工具类
 * 
 * @author zhu_longxiang
 *
 */
public class UnicodeUtil {

	/**
	 * 字符串转Unicode
	 * 
	 * @param string
	 *            字符串
	 * @return 字符串对应的unicode
	 */
	public static String str2Unicode(String string) {
		StringBuffer unicode = new StringBuffer();
		for (int i = 0; i < string.length(); i++) {
			char c = string.charAt(i);
			unicode.append("\\u" + Integer.toHexString(c));
		}
		return unicode.toString();
	}

	/**
	 * Unicode转字符串
	 * 
	 * @param unicode
	 *            Unicode
	 * @return unicode对应的字符串
	 */
	public static String unicode2Str(String unicode) {
		StringBuffer string = new StringBuffer();
		String[] hex = unicode.split("\\\\u");
		for (int i = 1; i < hex.length; i++) {
			// 转换出每一个代码点
			int data = Integer.parseInt(hex[i], 16);
			// 追加成string
			string.append((char) data);
		}
		return string.toString();
	}
}
